package com.blt.car.home.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.blt.car.R;
import com.blt.car.utils.StringUtils;

import org.w3c.dom.Text;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import blt.app_core.api.base.CoreManager;
import blt.app_core.api.message.MessageApi;
import blt.app_core.api.message.bean.MessageInfo;

/**
 * Created by xgp on 2017/6/19.
 */

public class MessageAdapter extends BaseAdapter {


    private LayoutInflater inflater;
    private List<MessageInfo> data = new ArrayList<>();
    private List<MessageInfo> removeItems = new ArrayList<>();
    private boolean isEdit;
    private boolean isSelectAll;
    private SparseArray<Boolean> sparseArray;

    public MessageAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        sparseArray = new SparseArray<>();
    }

    public void addData(List<MessageInfo> datas) {
        data.addAll(datas);
        init();
        notifyDataSetChanged();
    }

    public void clear(){
        data.clear();
    }

    private void init() {
        for (int i = 0; i < data.size(); i++) {
            sparseArray.put(i, false);
        }
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public MessageInfo getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_message_content, parent, false);
            holder = new ViewHolder();
            holder.cBox = (CheckBox) convertView.findViewById(R.id.item_msg_check);
            holder.mContent = (TextView) convertView.findViewById(R.id.item_message_context);
            holder.mDay = (TextView) convertView.findViewById(R.id.item_message_day);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (isEdit) {
            holder.cBox.setVisibility(View.VISIBLE);
            holder.cBox.setChecked(isSelectAll);
        } else {
            holder.cBox.setVisibility(View.GONE);
        }


        holder.mContent.setText(data.get(position).msgContent);
        holder.cBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                sparseArray.put(position, isChecked);
            }
        });
        holder.mDay.setText(StringUtils.getDay(data.get(position).updateTime));

//
//        convertView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });
        return convertView;
    }

    public void edit(boolean isEdit) {
        this.isEdit = isEdit;
        notifyDataSetChanged();
    }


    public void deleteItem() {
        for (int i = 0; i < sparseArray.size(); i++) {
            if (sparseArray.get(i)) {//选中的加入到删除项
                removeItems.add(data.get(i));
            }
        }
        for (MessageInfo info : removeItems) {
            CoreManager.getCore(MessageApi.class).deleteMsg(info.msgId);
        }
        data.removeAll(removeItems);
        notifyDataSetChanged();

    }

    public void selectAll(boolean isSelect) {
        isSelectAll = isSelect;
        removeItems.clear();
        if (isSelect) {
            removeItems.addAll(data);
        }
        notifyDataSetChanged();
    }

    class ViewHolder {
        CheckBox cBox;
        TextView mContent;
        TextView mDay;
    }


}
